home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zimage2.c < prev    next >
C/C++ Source or Header  |  1993-05-17  |  6KB  |  184 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zimage2.c */
  20. /* image operator extensions for Level 2 PostScript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gscolor.h"
  26. #include "gxcolor.h"        /* for gscolor2.h */
  27. #include "gscspace.h"
  28. #include "gscolor2.h"
  29. #include "gsmatrix.h"
  30. #include "dict.h"
  31. #include "dparam.h"
  32. #include "ilevel.h"
  33. #include "state.h"        /* for igs */
  34.  
  35. /* Imported from zpaint.c */
  36. extern int zimage_setup(P10(int width, int height, gs_matrix *pmat,
  37.   ref *sources, int bits_per_component,
  38.   int spread, const gs_color_space *pcs, int masked,
  39.   const float *decode, int npop));
  40.  
  41. /* Names of keys in image dictionaries: */
  42. static ref name_ImageType;
  43. static ref name_Width;
  44. static ref name_Height;
  45. static ref name_ImageMatrix;
  46. static ref name_MultipleDataSources;
  47. static ref name_DataSource;
  48. static ref name_BitsPerComponent;
  49. static ref name_Decode;
  50. static ref name_Interpolate;
  51.  
  52. /* Initialization */
  53. private void
  54. zimage2_init(void)
  55. {    static const names_def imn[] = {
  56.  
  57.     /* Create the names of the known entries in */
  58.     /* an image dictionary. */
  59.        { "ImageType", &name_ImageType },
  60.        { "Width", &name_Width },
  61.        { "Height", &name_Height },
  62.        { "ImageMatrix", &name_ImageMatrix },
  63.        { "MultipleDataSources", &name_MultipleDataSources },
  64.        { "DataSource", &name_DataSource },
  65.        { "BitsPerComponent", &name_BitsPerComponent },
  66.        { "Decode", &name_Decode },
  67.        { "Interpolate", &name_Interpolate },
  68.  
  69.     /* Mark the end of the initialized name list. */
  70.         names_def_end
  71.     };
  72.  
  73.     init_names(imn);
  74. }
  75.  
  76.  
  77. /* Define a structure for acquiring image parameters. */
  78. typedef struct image_params_s {
  79.     int Width;
  80.     int Height;
  81.     gs_matrix ImageMatrix;
  82.     int /*boolean*/ MultipleDataSources;
  83.     ref DataSource[4];
  84.     int BitsPerComponent;
  85.     float Decode[2*4];
  86.     int /*boolean*/ Interpolate;
  87. } image_params;
  88.  
  89. /* Common code for unpacking an image dictionary. */
  90. /* Assume *op is a dictionary. */
  91. private int
  92. image_dict_unpack(os_ptr op, image_params *pip, int max_bits_per_component)
  93. {    int code;
  94.     int num_components;
  95.     int decode_size;
  96.     ref *pds;
  97.     check_dict_read(*op);
  98.     num_components = gs_currentcolorspace(igs)->type->num_components;
  99.     if ( num_components < 1 )
  100.         return_error(e_rangecheck);
  101.     if ( (code = dict_int_param(op, &name_ImageType, 1, 1, 1,
  102.                     &code)) < 0 ||
  103.          (code = dict_int_param(op, &name_Width, 0, 0x7fff, -1,
  104.                     &pip->Width)) < 0 ||
  105.          (code = dict_int_param(op, &name_Height, 0, 0x7fff, -1,
  106.                     &pip->Height)) < 0 ||
  107.          (code = dict_matrix_param(op, &name_ImageMatrix,
  108.                     &pip->ImageMatrix)) < 0 ||
  109.          (code = dict_bool_param(op, &name_MultipleDataSources, 0,
  110.                     &pip->MultipleDataSources)) < 0 ||
  111.          (code = dict_int_param(op, &name_BitsPerComponent, 0,
  112.                     max_bits_per_component, -1,
  113.                     &pip->BitsPerComponent)) < 0 ||
  114.          (code = decode_size = dict_float_array_param(op, &name_Decode,
  115.                     num_components * 2,
  116.                     &pip->Decode[0], NULL)) < 0 ||
  117.          (code = dict_bool_param(op, &name_Interpolate, 0,
  118.                     &pip->Interpolate)) < 0
  119.        )
  120.         return code;
  121.     if ( decode_size != num_components * 2 )
  122.         return_error(e_rangecheck);
  123.     /* Extract and check the data sources. */
  124.     if ( (code = dict_find(op, &name_DataSource, &pds)) < 0 )
  125.         return code;
  126.     if ( pip->MultipleDataSources )
  127.     {    check_type(*pds, t_array);
  128.         if ( r_size(pds) != num_components )
  129.             return_error(e_rangecheck);
  130.         memcpy(&pip->DataSource[0], pds->value.refs, sizeof(ref) * num_components);
  131.     }
  132.     else
  133.         pip->DataSource[0] = *pds;
  134.     return 0;
  135. }
  136.  
  137. /* (<width> <height> <bits/sample> <matrix> <datasrc> image -) */
  138. /* <dict> image - */
  139. int
  140. z2image(register os_ptr op)
  141. {    if ( level2_enabled && r_has_type(op, t_dictionary) )
  142.     {    image_params ip;
  143.         int code = image_dict_unpack(op, &ip, 12);
  144.         if ( code < 0 )
  145.             return code;
  146.         return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  147.             &ip.DataSource[0], ip.BitsPerComponent,
  148.             0, gs_currentcolorspace(igs), 0, &ip.Decode[0], 1);
  149.     }
  150.     /* Level 1 image operator */
  151.     check_op(5);
  152.     return zimage(op);
  153. }
  154.  
  155. /* (<width> <height> <paint_1s> <matrix> <datasrc> imagemask -) */
  156. /* <dict> imagemask - */
  157. int
  158. z2imagemask(register os_ptr op)
  159. {    if ( level2_enabled && r_has_type(op, t_dictionary) )
  160.     {    image_params ip;
  161.         gs_color_space cs;
  162.         int code = image_dict_unpack(op, &ip, 1);
  163.         if ( code < 0 )
  164.             return code;
  165.         if ( ip.MultipleDataSources )
  166.             return_error(e_rangecheck);
  167.         cs.type = &gs_color_space_type_DeviceGray;
  168.         return zimage_setup(ip.Width, ip.Height, &ip.ImageMatrix,
  169.             &ip.DataSource[0], 1, 0, &cs, 1, &ip.Decode[0], 1);
  170.     }
  171.     /* Level 1 imagemask operator */
  172.     check_op(5);
  173.     return zimagemask(op);
  174. }
  175.  
  176. /* ------ Initialization procedure ------ */
  177.  
  178. /* Note that these override the definitions in zpaint.c. */
  179. op_def zimage2_op_defs[] = {
  180.     {"1image", z2image},
  181.     {"1imagemask", z2imagemask},
  182.     op_def_end(zimage2_init)
  183. };
  184.